﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Zsea.HTML;
using System.Drawing;

namespace Zsea.Browser
{
    public class RequestResult
    {
        // Fields
        private CookieContainer _c = new CookieContainer();
        private HttpStatusCode _HttpStateCode = HttpStatusCode.NoContent;
        private string _uri = string.Empty;

        private Document doc;
        private Image img = null;
        private string h = string.Empty;

        private byte[] _b = null;

        // Methods
        public Form GetFormSingle()
        {
            Document document = new Document();
            document.Load(this.Html);
            Node node = document.DocumentNode.SingleNode("form");
            if (node == null)
            {
                return null;
            }
            return new Form(node.OuterHTML);
        }

        public Form GetFormSingle(int index)
        {
            Document document = new Document();
            document.Load(this.Html);
            List<Node> list = document.DocumentNode.FindAll("form");
            if (index >= list.Count)
            {
                return null;
            }
            return new Form(list[index].OuterHTML);
        }

        public static bool IsNullOrEmpty(RequestResult hac)
        {
            return ((hac == null) || (((hac.HttpStatusCode != HttpStatusCode.OK) && (hac.HttpStatusCode != HttpStatusCode.Found)) || string.IsNullOrEmpty(hac.Html)));
        }

        // Properties
        public byte[] Bytes
        {
            get
            {
                return _b;
            }
            internal set
            {
                this._b = value;
            }
        }

        public CookieContainer CookieContainer
        {
            get
            {
                return this._c;
            }
            set
            {
                this._c = value;
            }
        }

        public Document Document
        {
            get
            {
                if (this.doc == null)
                {
                    this.doc = new Document();
                    this.doc.Load(this.Html);
                }
                return this.doc;
            }
        }

        public string GotoUrl
        {
            get
            {
                return this._uri;
            }
            set
            {
                this._uri = value;
            }
        }

        public string Html
        {
            get
            {
                return this.h;
            }
            internal set
            {
                this.h = value;
                this.doc = null;
                this.img = null;
            }
        }

        public Image Image
        {
            get
            {
                if (img == null)
                {
                    img = new Bitmap(new System.IO.MemoryStream(this._b));
                }
                return img;
            }
        }

        public HttpStatusCode HttpStatusCode
        {
            get
            {
                return this._HttpStateCode;
            }
            set
            {
                this._HttpStateCode = value;
            }
        }

        public WebExceptionStatus WebExceptionStatus
        {
            get;
            set;
        }
    }

}
